window.onload = rollover;

function rollover() {
	// 获取页面的所有<a>标签
	for (var i = 0; i < document.links.length; i++) {
		var linkObj = document.links[i];
		if (linkObj.id) {
			var imgObj = document.getElementById(linkObj.id + 'Img');
			if (imgObj) {
				setupRollover(linkObj, imgObj);
			}
		}
	}
}

function setupRollover(thisLink, thisImg) {

	// 创建两张图片
	thisLink.outImage = new Image();
	thisLink.outImage.src = thisImg.src;

	thisLink.overImage = new Image();
	thisLink.overImage.src = "../images/image2.jpg";

	// imgToChange和thisImg指向同一个Img对象
	thisLink.imgToChange = thisImg;
	thisLink.onmouseout = function() {
		this.imgToChange.src = this.outImage.src;
	};
	thisLink.onmouseover = function() {
		this.imgToChange.src = this.overImage.src;
	};
}